<?php

namespace ApiBundle\Controller\Admin;

use ApiBundle\Controller\BaseController;
use Symfony\Component\HttpFoundation\Request;

class CompanyController extends BaseController
{
    /*
     * 得到企业列表
     */
    public function getCompanyListAction(Request $request)
    {
        if(!$this->isAdminLogged()){
            return $this->admin_response;
        }

        $page = $request->get("page","1");

        $rows = $request->get("rows","10");

        // 搜索
        $credit_code = $request->get("credit_code");
        $name= $request->get("name");
        $fr_name = $request->get("fr_name");
        $user_company_personal = $request->get("user_name"); // 科技联络人
        $company_attribute_id = $request->get("company_attribute_id");
        $company_category_id = $request->get("company_category_id");
        $admin_id = $request->get("admin_id");

        $admin_role_id = $this->get("session")->get("admin_role_id");

        $sql = "SELECT c.* FROM company c ";

        if(!empty($user_company_personal)){
            $sql .= " LEFT JOIN user u ON c.id = u.company_id ";
        }


        if($admin_role_id == 4){

            $sql .= " WHERE c.admin_id = '". $this->getAdminId() ."' ";
        } else {
            $sql .= " WHERE c.district_id = '". $this->getDistrictId() ."'  ";
        }

        $conn = $this->get("database_connection");

        //总数
        $company_count = count($conn->fetchAll($sql));

        // 今日总数
        $curr_date = date('Y-m-d');
        $today_sql = $sql . " AND  DATE_FORMAT(c.created_at,'%Y-%m-%d') = '". $curr_date ."' ";
        $today_count = count($conn->fetchAll($today_sql));


        if(!empty($credit_code)){
            $sql .= " AND c.credit_code = '". $credit_code ."'  ";
        }

        if(!empty($name)){
            $sql .= " AND c.name = '". $name ."'  ";
        }

        if(!empty($fr_name)){
            $sql .= " AND c.fr_name = '". $fr_name ."'  ";
        }

        if(!empty($company_attribute_id)){
            $sql .= " AND c.company_attribute_id = '". $company_attribute_id ."'  ";
        }

        if(!empty($company_category_id)){
            $sql .= " AND c.company_category_id = '". $company_category_id ."'  ";
        }

        if(!empty($admin_id)){
            $sql .= " AND c.admin_id = '". $admin_id ."'  ";
        }

        if(!empty($user_company_personal)){
            $sql .= " AND u.technology_liaisons_status = 1 AND u.name = '". $user_company_personal ."'  ";
        }


        $sql .= " ORDER BY c.created_at DESC ";


        //带搜索条件下的总数
        $search_all_count = count($conn->fetchAll($sql));
        $all_page = ceil($search_all_count / $rows);

        // 分页
        $m = ($page -1) * $rows;

        $sql .= " LIMIT $m,$rows ";


        $data = $conn->fetchAll($sql);


        foreach($data as &$item){
            $admin = $conn->fetchAssoc("SELECT * FROM admin WHERE id = '". $item["admin_id"] ."'");

            if($admin['admin_role_id'] == 4){
                $item['admin'] = $admin['nickname'];
            } else {
                $item['admin'] = "无";
            }


            $company_attribute = $conn->fetchAssoc("SELECT company_attribute_name FROM company_attribute WHERE  id = '". $item["company_attribute_id"] ."'");

            $company_category = $conn->fetchAssoc("SELECT company_category_name FROM company_category WHERE id = '". $item["company_category_id"] ."' ");

            if($company_attribute){
                $item['company_attribute'] = $company_attribute['company_attribute_name'];
            } else {
                $item['company_attribute'] = "";
            }

            if($company_category){
                $item['company_category'] = $company_category['company_category_name'];

            } else {
                $item['company_category'] = "";
            }





        }

        return $this->jsonResponse(0,"操作成功",array("data"=>$data,"company_count"=>$company_count,"search_all_count"=>$search_all_count,"all_page"=>$all_page,"today_count"=>$today_count));

    }

    /*
     * 得到企业详情
     */
    public function getCompanyDetailAction(Request $request)
    {
        $id = $request->get("id");
        if(!$this->isAdminLogged()){
            return $this->admin_response;
        }
        if(empty($id)){
            return $this->jsonResponse(1,"参数错误");
        }

        $conn = $this->get("database_connection");
        $data = $conn->fetchAssoc("SELECT * FROM company WHERE id = '". $id ."'");
        return $this->jsonResponse(0,"操作成功",$data);
    }

    /*
     * 获取企业的科技联络人
     */
    public function companyTechnologicalAction(Request $request)
    {
        $id = $request->get("id");
        if(!$this->isAdminLogged()){
            return $this->admin_response;
        }
        if(empty($id)){
            return $this->jsonResponse(1,"参数错误");
        }

        $conn = $this->get("database_connection");
        $data = $conn->fetchAll("SELECT * FROM user WHERE company_id = '". $id ."' AND technology_liaisons_status = 1");
        return $this->jsonResponse(0,"操作成功",$data);
    }

    /*
     * 编辑企业信息
     */
    public function editCompanyMessageAction(Request $request)
    {
        if(!$this->isAdminLogged()){
            return $this->admin_response;
        }
        $id = $request->get("id");
        $medal = $request->get("medal_id");
        $event_type = $request->get("event_type");
        $event_content = $request->get("event_content");
        $admin_id = $request->get("admin_id");
        $company_attribute = $request->get("company_attribute_id");
        $company_category = $request->get("company_category_id");

//        if(empty($id) || empty($medal) || empty($event_type) || empty($event_content) || empty($admin_id) || empty($company_attribute) || empty($company_category)){
//
//            return $this->jsonResponse(1,"参数错误");
//        }

        if(empty($id) || empty($company_attribute) || empty($company_category)){

            return $this->jsonResponse(1,"参数错误");
        }

        $conn = $this->get("database_connection");

        // 添加 企业-勋章表
        if(!empty($medal)){
            $company_medal = array(
                "company_id" => $id,
                "medal_id" => $medal,
                "created_at" => date("Y-m-d H:i:s")
            );

            //删除企业之前的勋章
            $conn->delete("company_medal",array("company_id" => $id));

            $conn->insert("company_medal",$company_medal);
        }


        // 添加 事件表
        if(!empty($event_type)){

            if(empty($event_content)){
                return $this->jsonResponse(1,"请输入事件内容");
            }

            $event = array(
                "company_id" => $id,
                "type" => $event_type,
                "company_event_content" => $event_content
            );

            //删除企业之前的事件
            $conn->delete("company_event",array("company_id" => $id));

            $conn->insert("company_event",$event);

        }


        // 修改company表
        $company = array(
            "company_attribute_id" => $company_attribute,
            "company_category_id" => $company_category,
            "updated_at" => date("Y-m-d H:i:s")
        );

        if(!empty($admin_id)){
            $company['admin_id'] = $admin_id;
        }
        $where = array(
            "id" => $id
        );

        $conn->update("company",$company,$where);


        return $this->jsonResponse(0,"操作成功");
    }

    /*
     * 删除/恢复企业列表
     */
    public function changeStatusCompanyAction(Request $request)
    {
        $id = $request->get("id");
        $status = $request->get("status");
        if(empty($id)){
            return $this->jsonResponse(1,"参数错误");
        }

        $conn = $this->get("database_connection");

        // 修改企业状态
        $conn->update("company",array("status" => $status),array("id" => $id));

        // 修改人员状态
        $user_where = array(
            "company_id" => $id
        );

        $conn->update("user",array("status" => $status),$user_where);

        if($status == "0"){

            return $this->jsonResponse(0,"删除成功");
        } else if($status == "1"){

            return $this->jsonResponse(0,"恢复成功");
        } else {

            return $this->jsonResponse(1,"status错误");
        }
    }
}
